# """
# This is the interface that allows for creating nested lists.
# You should not implement it, or speculate about its implementation
# """
# class NestedInteger(object):
#    def isInteger(self):
#        """
#        @return True if this NestedInteger holds a single integer, rather than a nested list.
#        :rtype bool
#        """
#
#    def getInteger(self):
#        """
#        @return the single integer that this NestedInteger holds, if it holds a single integer
#        Return None if this NestedInteger holds a nested list
#        :rtype int
#        """
#
#    def getList(self):
#        """
#        @return the nested list that this NestedInteger holds, if it holds a nested list
#        Return None if this NestedInteger holds a single integer
#        :rtype List[NestedInteger]
#        """


class NestedIterator(object):
    def __init__(self, nestedList):
        self.lists = self.search(nestedList)
        self.length = len(self.lists)
        self.index = 0

    def search(self, list_in):
        mid_res = []
        for i in list_in:
            if isinstance(i, list):
                mid_res.extend(self.search(i))
            else:
                mid_res.append(i)
        return mid_res

    def next(self):
        self.index += 1
        return self.lists[self.index - 1]

    def hasNext(self):
        return self.index < self.length


class NestedIterator2(object):
    def __init__(self, nestedList):
        self.lists = self.search(nestedList)
        self.length = len(self.lists)
        self.index = 0

    def search(self, list_in):
        import re
        mid_res = str(list_in)
        mid_res = re.sub(r"[\[\]]", "", mid_res)
        mid_res = mid_res.split(",")
        return mid_res

    def next(self):
        self.index += 1
        return int(self.lists[self.index - 1])

    def hasNext(self):
        return self.index < self.length


"""
这两个传上去都莫名其妙报错,貌似python2对str的支持和对isinstance的支持有问题
"""

# Your NestedIterator object will be instantiated and called as such:
# i, v = NestedIterator(nestedList), []
# while i.hasNext(): v.append(i.next())


# a = NestedIterator([[1,1],2,[1,1]])
# print(a.lists)
# a = NestedIterator([1,[4,[6]]])
# print(a.lists)

i, v = NestedIterator([[1,1],2,[1,1]]), []
while i.hasNext():
    v.append(i.next())
print(v)
i, v = NestedIterator([1,[4,[6]]]), []
while i.hasNext():
    v.append(i.next())
print(v)
